package com.herench.bs.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.log4j.Logger;

import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;

/**
 * Created by ThinkPad on 2017/9/23.
 */
public class AuthHelper {

    private static Logger logger = Logger.getLogger(AuthHelper.class.getClass());


    public static Map<String, Object> sendLoginVerificationCode(String type, String url, String channelCode, String phone) throws Exception {
        Map<String,Object> res = new HashMap<>(16);
        JSONObject jsonObject = new JSONObject();
        Map<String, String> params = new TreeMap<>();
        params.put("sysCode",channelCode);
        params.put("phone",phone);
        String funcode = null;
        if ("1".equals(type)){
            funcode = "500001";
        }else if ("2".equals(type)){
            funcode="500002";
        }else if ("3".equals(type)){
            funcode="500003";
        }else if ("4".equals(type)){
            funcode="500004";
        }else {
            throw new IllegalAccessException("未开开放其他验证码");
        }
        jsonObject.put("funcode",funcode );
        jsonObject.put("token", "token");
        jsonObject.put("args", DesUtil.encrypt(JSON.toJSONString(params)));
        String str = null;
        try {
            str = HttpClientSupport.doPost(url, jsonObject.toString(), "UTF-8");
            JSONObject resJson = JSON.parseObject(str);
            res.put("code",resJson.getString("code"));
            res.put("message",resJson.getString("message"));
        } catch (Exception e) {
            res.put("code","1");
            res.put("message","发送验证码失败");
            logger.info("发送验证码失败："+e.getMessage());
            e.printStackTrace();
        }

        return  res;
    }


    public static Map<String,String> validateVerificationCode(String url,String channelCode,String phone,String vcode) throws Exception {
        Map<String,String> res = new HashMap<>(16);
        JSONObject jsonObject = new JSONObject();
        Map<String, String> params = new TreeMap<>();
        params.put("sysCode",channelCode);
        params.put("vcode",vcode);
        params.put("phone",phone);
        jsonObject.put("funcode", "500101");
        jsonObject.put("token", "token");
        jsonObject.put("args", DesUtil.encrypt(JSON.toJSONString(params)));
        String str = HttpClientSupport.doPost(url, jsonObject.toString(), "UTF-8");
        JSONObject resJson = JSON.parseObject(str);
        res.put("code",resJson.getString("code"));
        res.put("message",resJson.getString("message"));
        return  res;
    }
}
